//
//  PSSelectJobCategoryView.m
//  Btpin
//
//  Created by Vic on 2019/5/30.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "PSSelectJobCategoryView.h"
#import "PSSelectJobCategoryCell.h"

@interface PSSelectJobCategoryView()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSArray *dataArray;

//列表
@property (nonatomic, strong) UITableView *leftTable;
@property (nonatomic, strong) UITableView *middleTable;
@property (nonatomic, strong) UITableView *rightTable;

//定位
@property (nonatomic, assign) NSInteger leftIndex;
@property (nonatomic, assign) NSInteger middleIndex;
@property (nonatomic, assign) NSInteger rightIndex;

@end

@implementation PSSelectJobCategoryView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.dataArray = [DataToolsControl sharedManager].allCategoryInfoModel.jobCatsArrayForOneLevel;
        [self createSubview];
    }
    return self;
}

- (void)createSubview {
    UILabel *topLbl = [UIToolsControl initLabelWithFrame:CGRectMake(0, 0, WIDTH, 0.0001) text:@"  选择您的职位类型" textColor:THEMECOLOR_OF_TEXT_333333 font:Font_20];
    [self addSubview:topLbl];
    [topLbl borderColor:THEMECOLOR_OF_TEXT_cccccc borderWidth:1 borderType:BorderSideTypeBottom];
    
    CGFloat width = WIDTH/3;
    
    self.leftTable = [[UITableView alloc] initWithFrame:CGRectMake(0, EndY(topLbl), width, self.frame.size.height-ViewHeight(topLbl)) style:UITableViewStylePlain];
    self.leftTable.delegate = self;
    self.leftTable.dataSource = self;
    self.leftTable.showsVerticalScrollIndicator = NO;
    self.leftTable.rowHeight = 45;
    self.leftTable.tableFooterView = [[UIView alloc] init];
    self.leftTable.backgroundColor = COLOR_OF_WHITE;
    [self addSubview:self.leftTable];
    [self.leftTable registerClass:[PSSelectJobCategoryCell class] forCellReuseIdentifier:isCellID];
    self.leftIndex = 0;
    
    self.middleTable = [[UITableView alloc] initWithFrame:CGRectMake(width, EndY(topLbl), width, self.frame.size.height-ViewHeight(topLbl)) style:UITableViewStylePlain];
    self.middleTable.delegate = self;
    self.middleTable.dataSource = self;
    self.middleTable.showsVerticalScrollIndicator = NO;
    self.middleTable.rowHeight = 45;
    self.middleTable.tableFooterView = [[UIView alloc] init];
    self.middleTable.backgroundColor = COLOR_OF_WHITE;
    [self addSubview:self.middleTable];
    [self.middleTable registerClass:[PSSelectJobCategoryCell class] forCellReuseIdentifier:isCellID];
    self.middleIndex = 0;
    
    self.rightTable = [[UITableView alloc] initWithFrame:CGRectMake(2*width, EndY(topLbl), width, self.frame.size.height-ViewHeight(topLbl)) style:UITableViewStylePlain];
    self.rightTable.delegate = self;
    self.rightTable.dataSource = self;
    self.rightTable.showsVerticalScrollIndicator = NO;
    self.rightTable.rowHeight = 45;
    self.rightTable.tableFooterView = [[UIView alloc] init];
    self.rightTable.backgroundColor = COLOR_OF_WHITE;
    [self addSubview:self.rightTable];
    [self.rightTable registerClass:[PSSelectJobCategoryCell class] forCellReuseIdentifier:isCellID];
    self.rightIndex = 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == self.leftTable) {
        return self.dataArray.count;
    } else if(tableView == self.middleTable) {
        PublicCategoryElementModel *model = self.dataArray[self.leftIndex];
        NSArray *secArray = model.jobCatsArrayForTworThreeLevel;
        return secArray.count;
    } else {
        PublicCategoryElementModel *model = self.dataArray[self.leftIndex];
        NSArray *secArray = model.jobCatsArrayForTworThreeLevel;
        PublicCategoryElementModel *model222 = secArray[self.middleIndex];
        NSArray *thirdArray = model222.jobCatsArrayForTworThreeLevel;
        return thirdArray.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PSSelectJobCategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:isCellID forIndexPath:indexPath];
    
    if (tableView == self.leftTable) {
        [cell setData:self.dataArray[indexPath.row] isSelect:(self.leftIndex == indexPath.row)? YES : NO];
    } else if(tableView == self.middleTable) {
        PublicCategoryElementModel *model = self.dataArray[self.leftIndex];
        NSArray *secArray = model.jobCatsArrayForTworThreeLevel;
        [cell setData:secArray[indexPath.row] isSelect:(self.middleIndex == indexPath.row)? YES : NO];
    } else {
        PublicCategoryElementModel *model = self.dataArray[self.leftIndex];
        NSArray *secArray = model.jobCatsArrayForTworThreeLevel;
        PublicCategoryElementModel *model333 = secArray[self.middleIndex];
        NSArray *thirdArray = model333.jobCatsArrayForTworThreeLevel;
        [cell setData:thirdArray[indexPath.row] isSelect:(self.rightIndex == indexPath.row)? YES : NO];
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.00001;
}
#pragma mark -- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == self.leftTable) {
        if (indexPath.row != self.leftIndex) {
            self.leftIndex = indexPath.row;
            [self.leftTable reloadData];
            self.middleIndex = 0;
            [self.middleTable reloadData];
            self.rightIndex = 0;
            [self.rightTable reloadData];
        }
    } else if(tableView == self.middleTable) {
        if (indexPath.row != self.middleIndex) {
            self.middleIndex = indexPath.row;
            [self.middleTable reloadData];
            self.rightIndex = 0;
            [self.rightTable reloadData];
        }
    } else {
        self.rightIndex = indexPath.row;
        [self.rightTable reloadData];
    }
}
#pragma mark -- Action
- (PublicCategoryElementModel *)getOneLevelInfo {
    PublicCategoryElementModel *model = self.dataArray[self.leftIndex];
    return model;
}
- (PublicCategoryElementModel *)getTwoLevelInfo {
    PublicCategoryElementModel *model = self.dataArray[self.leftIndex];
    NSArray *cityArray = model.jobCatsArrayForTworThreeLevel;
    PublicCategoryElementModel *model2222 = cityArray[self.middleIndex];
    return model2222;
}
- (PublicCategoryElementModel *)getThreeLevelInfo {
    PublicCategoryElementModel *model = self.dataArray[self.leftIndex];
    NSArray *cityArray = model.jobCatsArrayForTworThreeLevel;
    PublicCategoryElementModel *model2222 = cityArray[self.middleIndex];
    NSArray *areaArray = model2222.jobCatsArrayForTworThreeLevel;
    PublicCategoryElementModel *model3333 = areaArray[self.middleIndex];
    return model3333;
}
- (SelectJobCategoryModel *)getAllInfo {
    SelectJobCategoryModel *returnModel = [[SelectJobCategoryModel alloc] init];
    
    PublicCategoryElementModel *model = self.dataArray[self.leftIndex];
    returnModel.oneLevelName = model.name;
    returnModel.oneLevelID = model.id;
    
    NSArray *cityArray = model.jobCatsArrayForTworThreeLevel;
    PublicCategoryElementModel *model222 = cityArray[self.middleIndex];
    returnModel.twoLevelName = model222.name;
    returnModel.twoLevelID = model222.id;
    
    NSArray *areaArray = model222.jobCatsArrayForTworThreeLevel;
    PublicCategoryElementModel *model333 = areaArray[self.rightIndex];
    returnModel.threeLevelName = model333.name;
    returnModel.threeLevelID = model333.id;
    return returnModel;
}

@end
